<%--
  Created by IntelliJ IDEA.
  User: qingx
  Date: 2018/11/28
  Time: 17:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- jsp文件头和头部 -->
    <%@ include file="../base/top.jsp" %>
    <link rel="stylesheet" href="static/ace/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />
</head>

<body class="no-skin">
<div class="main-container ace-save-state" id="main-container">
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <div class="row">
                    <div class="col-xs-12" id="add_teacher">
                        <!-- PAGE CONTENT BEGINS -->

                        <form class="form-horizontal" role="form" style="display: -webkit-flex;-webkit-justify-content: center;-webkit-align-items: center;">

                            <div style="width: 70%;">
                                <div class="widget-box">

                                    <div class="widget-header" style="text-align: center">
                                        <h4 class="widget-title">添加教师</h4>

                                    </div>

                                    <div class="widget-main">

                                        <div>
                                            <label >录入类型</label>
                                            <br />
                                            <select class="chosen-select form-control" id="add-type"
                                                    onchange="addTeacherType(this[selectedIndex].value)" data-placeholder="Choose a State...">
                                                <option></option>
                                                <option selected="selected" value="0">录入新用户</option>
                                                <option value="1">录入已存在用户</option>
                                            </select>
                                        </div>
                                        <hr />

                                        <div id="user" style="display: none">
                                            <label >用户id</label>
                                            <br />
                                            <input class="form-control" id="user-id" onchange="userIdMessage(this.value)">
                                            <hr />
                                        </div>

                                        <div>
                                            <label >工号</label>
                                            <br />
                                            <input class="form-control" id="tea-id">
                                        </div>
                                        <hr />

                                        <div>
                                            <label >姓名</label>
                                            <br />
                                            <input class="form-control" id="tea-name">
                                        </div>
                                        <hr />

                                        <div>
                                            <label >性别</label>
                                            <br />
                                            <select class="chosen-select form-control" id="tea-sex" data-placeholder="Choose a State...">
                                                <option></option>
                                                <option selected="selected" value="0">男</option>
                                                <option value="1">女</option>
                                            </select>
                                        </div>
                                        <hr />

                                        <div>
                                            <label >出生日期</label>
                                            <br />
                                            <div class="input-group">
                                                <input class="form-control date-picker" id="tea-birth" type="text" readonly data-date-format="yyyy-mm-dd" />
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar bigger-110"></i>
                                                </span>
                                            </div>
                                        </div>
                                        <hr />

                                        <div>
                                            <label >入职年份</label>
                                            <br />
                                            <input type="text" class="form-control form-filter yearpicker" readonly id="tea-entry-year" />
                                        </div>
                                        <hr />


                                        <div>
                                            <label >就职状态</label>
                                            <br />
                                            <select class="chosen-select form-control" id="tea-work-state"  data-placeholder="Choose a State...">
                                                <option selected="selected">在职</option>
                                                <option>离职</option>
                                                <option>出差</option>
                                                <option>外派</option>
                                                <option>退休</option>
                                                <option>请假</option>
                                            </select>
                                        </div>
                                        <hr />

                                        <div>
                                            <label >联系方式</label>
                                            <br />
                                            <input class="form-control" maxlength="11" id="tea-telephone" oninput="value=value.replace(/[^\d]/g,'')">
                                        </div>
                                        <hr />

                                        <div>
                                            <label >学历</label>
                                            <br />
                                            <select class="chosen-select form-control" id="tea-education" data-placeholder="Choose a State...">
                                                <option></option>
                                                <option>学士</option>
                                                <option>硕士</option>
                                                <option>博士</option>
                                            </select>
                                        </div>
                                        <hr />


                                        <div>
                                            <label >职位</label>
                                            <br />
                                            <select class="chosen-select form-control" id="tea-position" data-placeholder="Choose a State...">
                                                <option></option>
                                                <option>讲师</option>
                                                <option>教授</option>
                                                <option>副教授</option>
                                            </select>
                                        </div>

                                        <div class="clearfix form-actions" align="center">
                                            <button class="btn btn-info" type="button" onclick="add()">
                                                Submit
                                            </button> &nbsp; &nbsp; &nbsp;
                                            <button class="btn" type="reset">
                                                Reset
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>

                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->
    <!-- 返回顶部 -->
    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
    </a>
</div>
<!-- basic scripts -->
<!-- 页面底部js¨ -->
<%@ include file="../base/foot.jsp"%>
<script type="text/javascript">
  /*  $(top.hangge());//关闭加载状态
*/

    $(function () {
        //日期框
        $('.date-picker').datepicker({autoclose: true, todayHighlight: true,endDate : new Date()});

        //年份选择框
        $('.yearpicker').datepicker({
            startView: 'decade',
            minView: 'decade',
            format: 'yyyy',
            maxViewMode: 2,
            minViewMode:2,
            autoclose: true,
            endDate : new Date()
        });

        //下拉框
        if (!ace.vars['touch']) {
            $('.chosen-select').chosen({allow_single_deselect: true});
            $(window)
                .off('resize.chosen')
                .on('resize.chosen', function () {
                    $('.chosen-select').each(function () {
                        var $this = $(this);
                        $this.next().css({'width': $this.parent().width()});
                    });
                }).trigger('resize.chosen');
            $(document).on('settings.ace.chosen', function (e, event_name, event_val) {
                if (event_name != 'sidebar_collapsed') return;
                $('.chosen-select').each(function () {
                    var $this = $(this);
                    $this.next().css({'width': $this.parent().width()});
                });
            });
            $('#chosen-multiple-style .btn').on('click', function (e) {
                var target = $(this).find('input[type=radio]');
                var which = parseInt(target.val());
                if (which == 2) $('#form-field-select-4').addClass('tag-input-style');
                else $('#form-field-select-4').removeClass('tag-input-style');
            });
        }
    });

  //根据添加用户类型，决定用户id项显示与否
  function addTeacherType(values){
      if(values == "0") {  //录入新用户
          document.getElementById('user').style.display = 'none';
      }else if(values == "1") {  //录入已存在用户
          document.getElementById('user').style.display = '';
      }
  }

  //输入用户id后，检查userId是否合法
  function userIdMessage(userId){
      console.log(userId);
      if(userId.length == 16){
          var data = {
              userId:userId
          }
          $.ajax({
              url : "<%=basePath%>user/state",
              type : "post",
              dataType : "json",
              data : data,
              success:function (data) {
                  if(data["success"]==true){
                      prompt_alert('success',data["msg"],0);
                  }else{
                      prompt_alert('error',data["msg"],0);
                  }
              },
              error:function(msg){
                  prompt_alert('error',"用户id查询失败！",0);
              }
          });
      }else {
          prompt_alert('warning',"用户id错误！",0);
      }
  }

  //数据校验
  function checkFormats() {
      if($("#add-type").val() =="") {
          prompt_alert('warning',"录入类型不得为空",0);
          return false;
      }
      if($("#tea-id").val() =="") {
          prompt_alert('warning',"教师工号不得为空",0);
          return false;
      }
      if($("#tea-name").val() =="") {
          prompt_alert('warning',"教师姓名不得为空",0);
          return false;
      }
      if($("#tea-sex").val() =="") {
          prompt_alert('warning',"教师性别不得为空",0);
          return false;
      }
      if($("#tea-birth").val() =="") {
          prompt_alert('warning',"出生日期不得为空",0);
          return false;
      }
      if($("#tea-entry-year").val() =="") {
          prompt_alert('warning',"入职年份不得为空",0);
          return false;
      }
      if($("#tea-work-state").val() =="") {
          prompt_alert('warning',"就职状态不得为空",0);
          return false;
      }
      if($("#tea-telephone").val() =="") {
          prompt_alert('warning',"联系方式不得为空",0);
          return false;
      }
      if($("#tea-education").val() =="") {
          prompt_alert('warning',"教师学历不得为空",0);
          return false;
      }
      if($("#tea-position").val() =="") {
          prompt_alert('warning',"教师职位不得为空",0);
          return false;
      }
      if(!tab($("#tea-birth").val(),$("#tea-entry-year").val())) {
          prompt_alert('warning',"出生日期与入职年份时间有误",0);
          return false;
      }
      return true;
  }

  function tab(date1,date2){
      var oDate1 = new Date(date1);
      var oDate2 = new Date(date2);
      if(oDate1.getTime() > oDate2.getTime()){
          return false;
      } else {
          return true;
      }
  }

  //添加
  function add(){
      if(checkFormats()){
          var type = null;
          if($("#add-type").val() == 0){
              type = "addNewUser";
          }else if($("#add-type").val() == 1) {
              type = "addExistingUser";
          }

          var data = {
              addType:type,
              userId:$("#user-id").val(),
              teaId:$("#tea-id").val(),
              teaName:$("#tea-name").val(),
              teaSex:$("#tea-sex").val(),
              teaBirth:$("#tea-birth").val(),
              teaEntryYear:$("#tea-entry-year").val(),
              teaWorkState:$("#tea-work-state").val(),
              teaTelephone:$("#tea-telephone").val(),
              teaEducation:$("#tea-education").val(),
              teaPosition:$("#tea-position").val(),
          }
          console.log(data);
          $.ajax({
              url : "<%=basePath%>teacher/addTeacherInfo",
              type : "post",
              dataType : "json",
              data : data,
              success:function (data) {
                  if(data["success"]==true){
                      prompt_alert('success',data["msg"],0);
                  }else{
                      prompt_alert('error',data["msg"],0);
                  }
              },
              error:function(msg){
                  prompt_alert('error',"教师录入失败！",0);
              }
          });
      }
  }
</script>
</body>
</html>
